/// @tags: GeneratingFunction
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long LL;
typedef unsigned long long ULL;

namespace Mker {

ULL SA, SB, SC;

inline void init() { cin >> SA >> SB >> SC; }

inline ULL rand() {
  SA ^= SA << 32, SA ^= SA >> 13, SA ^= SA << 1;
  ULL t = SA;
  SA = SB, SB = SC, SC ^= t ^ SA;
  return SC;
}

}  // namespace Mker

int const N = 65536 + 5, mod = 1e9 + 7, alpha = 233230706, base1 = 94153035,
          base2 = 905847205, exp1 = 64353223, exp2 = 847809841;

int f_1[N], f_2[N], f_3[N], f_4[N], T, ans;

inline int qpow1(int x) { return (LL)f_3[x >> 16] * f_1[x & 65535] % mod; }

inline int qpow2(int x) { return (LL)f_4[x >> 16] * f_2[x & 65535] % mod; }

inline int main() {
  f_1[0] = f_2[0] = f_3[0] = f_4[0] = 1;
  for (int i = 1; i < 65536; ++i) f_1[i] = (LL)f_1[i - 1] * base1 % mod;
  for (int i = 1; i < 65536; ++i) f_2[i] = (LL)f_2[i - 1] * base2 % mod;
  for (int i = 1; i < 65536; ++i) f_3[i] = (LL)f_3[i - 1] * exp1 % mod;
  for (int i = 1; i < 65536; ++i) f_4[i] = (LL)f_4[i - 1] * exp2 % mod;
  cin >> T;
  Mker::init();
  ULL n;
  while (T--)
    n = Mker::rand() % (mod - 1), ans ^= (LL)alpha * (qpow1(n) - qpow2(n) + mod) % mod;
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P5110 块速递推.in", "r", stdin);
  freopen("P5110 块速递推.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
